package com.t2t.top.devops.model;

import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class ResponseDTO<T> {

    private static final long serialVersionUID = 1L;

    /**
     * msg 默认成功信息
     */
    public static final String SUCCESS_MESSAGE = "ok";

    private static final String ERROR_MESSAGE = "error";


    /**
     * op 默认成功操作
     */
    public static final int DEFAULT_SUCCESS_CODE = 200;

    /**
     * 服务器日期 yyyy-MM-dd HH:mm:ss
     */
    private String servDate;

    /**
     * 服务器时间戳
     */
    private long servTimestamp;

    /**
     * 异常代码
     */
    private int code;

    /**
     * 异常信息
     */
    private String msg;

    /**
     * 返回结果
     */
    private T data;

    public static ResponseDTO success() {
        ResponseDTO dto = new ResponseDTO();
        dto.setCode(DEFAULT_SUCCESS_CODE);
        dto.setMsg(SUCCESS_MESSAGE);
        return dto;
    }

    public static ResponseDTO failure() {
        ResponseDTO dto = new ResponseDTO();
        dto.setCode(-1);
        dto.setMsg(ERROR_MESSAGE);
        return dto;
    }

    public ResponseDTO msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public ResponseDTO data(T data) {
        this.setData(data);
        return this;
    }

    public ResponseDTO code(Integer code) {
        this.setCode(code);
        return this;
    }

    public ResponseDTO() {
        Date now = new Date();
        this.servDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        this.servTimestamp = now.getTime();
    }


}
